const Proxy = require('../../proxy')
const webDataUtil = require('../../util/webDataUtil')
const reqlib = require('app-root-path').require
const btbAs = reqlib('/app/common/btb')

const BtbIndex = Proxy.BtbIndex

function parseValue (val) {
  return parseFloat(val || 0) || 0
}

// 获取合约账户财务信息
module.exports = async function () {
  const btbList = await BtbIndex.findWidthField({}, { code: 1 }, {
    sort: '-proportion'
  })
  // 现货
  const res = await webDataUtil.binance('getExchangeInfo')
  const resData = res.data || {}
  const symbols = resData.symbols || []
  // 合约
  const fres = await webDataUtil.binance('getFuturesExchangeInfo')
  const fresData = fres.data || {}
  const fsymbols = fresData.symbols || []

  for (let i = 0; i < btbList.length; i++) {
    const item = btbList[i]
    let exInfo = btbAs.getEXInfo(symbols, item.code)
    let fexInfo = btbAs.getEXInfo(fsymbols, item.code)
    await BtbIndex.update({
      code: item.code
    }, {
      min_qty: parseValue(exInfo.minQty) || 0,
      max_qty: parseValue(exInfo.maxQty) || 0,
      step_size: parseValue(exInfo.stepSize) || 0,
      min_notional: parseValue(exInfo.minNotional) || 0,
      tick_size: parseValue(exInfo.tickSize) || 0,
      f_min_qty: parseValue(fexInfo.minQty) || 0,
      f_max_qty: parseValue(fexInfo.maxQty) || 0,
      f_step_size: parseValue(fexInfo.stepSize) || 0,
      f_min_notional: parseValue(fexInfo.notional) || 0,
      f_tick_size: parseValue(fexInfo.tickSize) || 0
    })
    exInfo = null
    fexInfo = null
  }
}
